﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Slippor.Common.PluginFramework;
using RestoreHandlerPlugin.Properties;
using System.IO;

namespace RestoreHandlerPlugin
{
    public partial class ConfigForm : Form
    {
        private readonly IConfig _config;
        private Settings settings = RestoreConfig.SettingManager.Settings as Settings;
        List<RadioButton> _radioButtons;
        public ConfigForm(IConfig config)
        {
            _config = config;
            InitializeComponent();
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            _radioButtons = new List<RadioButton> { radioButtonSong, radioButtonArtistsSong, radioButtonSongArtists };
            InitializeConfigs();
        }

        private void InitializeConfigs()
        {
            textBoxPath.Text = settings.Path;
            bool normal = false;
            foreach (RadioButton rb in _radioButtons)
            {
                if (rb.Tag.ToString() == settings.NameFormate)
                {
                    normal = true;
                    rb.Checked = true;
                    break;
                }
            }
            if (!normal)
            {
                radioButtonCustom.Checked = true;
                textBoxCustom.Text = settings.NameFormate;
            }
            checkBoxSaveWithArtist.Checked = checkBoxDirWithAlbum.Enabled = ((DirOptions)settings.DirOptions & DirOptions.WithArtists) == DirOptions.WithArtists;
            checkBoxDirWithAlbum.Checked = ((DirOptions)settings.DirOptions & DirOptions.WithAlbum) == DirOptions.WithAlbum;
            checkBoxCover.Checked = checkBoxEmbeddedCover.Enabled = settings.SaveCover;
            checkBoxEmbeddedCover.Checked = settings.EmbeddedCover;
            checkBoxOnlyFav.Checked = settings.OnlyFav;
            checkBoxOnlyFull.Checked = settings.OnlyFull;
        }

        private void ConfirmConfigs()
        {
            settings.Path = textBoxPath.Text;
            if (radioButtonCustom.Checked)
            {
                settings.NameFormate = textBoxCustom.Text;
            }
            else
            {
                foreach (RadioButton rb in _radioButtons)
                {
                    if (rb.Checked)
                    {
                        settings.NameFormate = rb.Tag.ToString();
                        break;
                    }
                }
            }
            settings.DirOptions = (int)DirOptions.WithArtists * Convert.ToInt16(checkBoxSaveWithArtist.Checked) | (int)DirOptions.WithAlbum * Convert.ToInt16(checkBoxDirWithAlbum.Enabled && checkBoxDirWithAlbum.Checked);
            settings.OnlyFav = checkBoxOnlyFav.Checked;
            settings.OnlyFull = checkBoxOnlyFull.Checked;
            settings.SaveCover = checkBoxCover.Checked;
            settings.EmbeddedCover = settings.SaveCover && checkBoxEmbeddedCover.Checked;
            settings.Save();
        }

        private void buttonConfirm_Click(object sender, EventArgs e)
        {
            ConfirmConfigs();
            DialogResult = DialogResult.OK;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void buttonReset_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("确定重置？", "提示", MessageBoxButtons.OKCancel);
            if (result == DialogResult.OK)
            {
                settings.Reset();
                settings.Save();
                InitializeConfigs();
            }
        }

        private void buttonBrowse_Click(object sender, EventArgs e)
        {
            folderBrowserDialog.SelectedPath = Directory.CreateDirectory(settings.Path).FullName;
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                textBoxPath.Text = folderBrowserDialog.SelectedPath;
            }
        }

        private void radioButtonCustom_CheckedChanged(object sender, EventArgs e)
        {
            textBoxCustom.Enabled = radioButtonCustom.Checked;
        }

        private void checkBoxSaveWithArtist_CheckedChanged(object sender, EventArgs e)
        {
            checkBoxDirWithAlbum.Enabled = checkBoxSaveWithArtist.Checked;
        }

        private void buttonOpen_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("explorer.exe", textBoxPath.Text);
        }

        private void checkBoxCover_CheckedChanged(object sender, EventArgs e)
        {
            checkBoxEmbeddedCover.Enabled = checkBoxCover.Checked;
        }
    }
}
